﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.ProfessionalTeam.DAL;

public partial class MapInfo_ProfessionalTeam_SodiersNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //返回上一页
            if (Request.UrlReferrer != null)
            {
                ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
            }
            GetGuidList();
            Bind(0);
        }
    }
    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        UpdateGuidList();
        Bind(PageIndex);
    }

    void UpdateGuidList()
    {
        List<Guid> list = new List<Guid>();
        if (ViewState["GuidList"] != null)
        {
            list = ViewState["GuidList"] as List<Guid>;
        }
        for (int i = 0; i < gvSoldiers.Rows.Count; i++)
        {
            CheckBox CheckItem = gvSoldiers.Rows[i].FindControl("CheckItem") as CheckBox;
            Label lblID = gvSoldiers.Rows[i].FindControl("lblID") as Label;
            Guid id = new Guid(lblID.Text);

            if (CheckItem.Checked)
            {
                if (list.Contains(id))
                {

                }
                else
                {
                    list.Add(id);
                }

            }
            else
            {
                if (list.Contains(id))
                {
                    list.Remove(id);
                }
                else
                {

                }
            }

        }
        ViewState["GuidList"] = list;
    }

    void GetGuidList()
    {
        string DepartmentId = Request["DepartmentId"];
        //---------------------获取要显示的数据（勾选）1.抢险救援班2.灭火抢险救援专来队伍3.灭火攻坚组-------
        TeamDataContext tdc = new TeamDataContext();
        string Type = Request["Type"];
        //1.抢险救援班
        if (Type == "0")
        {
            List<Guid> pp = (from o in tdc.HelpSoldiers where o.DepartmentId == DepartmentId select o.SoldierId.Value).ToList<Guid>();
            ViewState["GuidList"] = pp;
        }
        else if (Type == "1")//2.灭火抢险救援专来队伍
        {
            Guid pid = new Guid(Request["Pid"]);
            List<Guid> pp = (from o in tdc.ProsSoldiers where o.Pid == pid select o.SoldierId.Value).ToList<Guid>();
            ViewState["GuidList"] = pp;
        }
        else if (Type == "2")//3.灭火攻坚组
        {
            Guid Aid = new Guid(Request["Aid"]);
            List<Guid> pp = (from o in tdc.AssaultSoldiers where o.AId == Aid select o.SoldierId.Value).ToList<Guid>();
            ViewState["GuidList"] = pp;
        }
    }

    void Bind(int PageIndex)
    {
        string DepartmentId = Request["DepartmentId"];

        //---------------------显示所有士兵--------------------------------------------------

        string where = " DepartmentID='" + DepartmentId + "' and IsDelete=0 ";
        Soldier soldier = new Soldier();
        int count = soldier.GetSoldiersCountByWhere(where);
        List<SoldierArchiveInfo> List = soldier.GetSoldiersByWhere(where, "IsContract", "ASC", 20, PageIndex);
        ListPageContral1.ItemCount = count;
        gvSoldiers.DataSource = List;
        gvSoldiers.DataBind();
    }


    protected void gvSoldiers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            List<Guid> list = new List<Guid>();
            if (ViewState["GuidList"] != null)
            {
                list = ViewState["GuidList"] as List<Guid>;
            }
            SoldierArchiveInfo info = e.Row.DataItem as SoldierArchiveInfo;
            if (list.Contains<Guid>(new Guid(info.SoldierID)))
            {
                CheckBox CheckItem = e.Row.FindControl("CheckItem") as CheckBox;
                CheckItem.Checked = true;
            }
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (ListPageContral1.CurrentPageIndex * 20 + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            System.Web.UI.HtmlControls.HtmlAnchor AName = e.Row.FindControl("AName") as System.Web.UI.HtmlControls.HtmlAnchor;
            Label lblId = e.Row.FindControl("lblID") as Label;
            Soldier soldierOp = new Soldier();
            SoldierArchiveInfo soldier = soldierOp.GetSoldierInfoByID(lblId.Text);
            AName.InnerText = soldier.SoldierName;
            //ImageButton btnEdit = e.Row.FindControl("btnEdit") as ImageButton;
            if (soldier.IsContract)
            {
                //AName.HRef = "javascript:OpenWin('EmployeeView.aspx?ID=" + soldier.SoldierID + "')";

                AName.HRef = "../../BasicBase/SoldierArchive/EmployeeView.aspx?ID=" + soldier.SoldierID;
                // btnEdit.PostBackUrl = "NewEmployee.aspx?ID=" + soldier.SoldierID;
            }
            else
            {
                //AName.HRef = "javascript:OpenWin('SoldierView1.aspx?ID=" + soldier.SoldierID + "')";
                AName.HRef = "../../BasicBase/SoldierArchive/SoldierView1.aspx?ID=" + soldier.SoldierID;
                //btnEdit.PostBackUrl = "NewSoldier.aspx?ID=" + soldier.SoldierID;
            }
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        UpdateGuidList();
        string DepartmentId = Request["DepartmentId"];
        TeamDataContext tdc = new TeamDataContext();
        List<Guid> list = new List<Guid>();
        if (ViewState["GuidList"] != null)
        {
            list = ViewState["GuidList"] as List<Guid>;
        }
        //1.抢险救援班
        string Type = Request["Type"];
        if (Type == "0")
        {
            if (list.Count > 0)
            {
                //删除全部
                List<HelpSoldiers> dsoldiers = tdc.HelpSoldiers.Where(o => o.DepartmentId == DepartmentId).ToList<HelpSoldiers>();
                tdc.HelpSoldiers.DeleteAllOnSubmit<HelpSoldiers>(dsoldiers);
                tdc.SubmitChanges();
                //重新添加
                List<HelpSoldiers> soldiers = new List<HelpSoldiers>();
                for (int i = 0; i < list.Count; i++)
                {
                    HelpSoldiers soldier = new HelpSoldiers();
                    soldier.DepartmentId = DepartmentId;
                    soldier.Id = Guid.NewGuid();
                    soldier.Lastdate = DateTime.Now;
                    soldier.SoldierId = list[i];
                    soldier.UpdataUser = User.Identity.Name;
                    soldiers.Add(soldier);
                }
                tdc.HelpSoldiers.InsertAllOnSubmit<HelpSoldiers>(soldiers);
                tdc.SubmitChanges();
            }
            string url = "HelpNew.aspx?Type=" + Request["Type"] + "&DepartmentId=" + Request["DepartmentId"] + "";
            Response.Redirect(url);
        }
        else if (Type == "1")
        {
            Guid pid = new Guid(Request["Pid"]);
            //删除全部
            List<ProsSoldiers> dsoldiers = tdc.ProsSoldiers.Where(o => o.Pid == pid).ToList<ProsSoldiers>();
            tdc.ProsSoldiers.DeleteAllOnSubmit<ProsSoldiers>(dsoldiers);
            tdc.SubmitChanges();
            //重新添加
            List<ProsSoldiers> soldiers = new List<ProsSoldiers>();
            for (int i = 0; i < list.Count; i++)
            {
                ProsSoldiers soldier = new ProsSoldiers();

                soldier.Id = Guid.NewGuid();
                soldier.LastDate = DateTime.Now;
                soldier.SoldierId = list[i];
                soldier.Pid = pid;
                soldiers.Add(soldier);
            }
            tdc.ProsSoldiers.InsertAllOnSubmit<ProsSoldiers>(soldiers);
            tdc.SubmitChanges();
            string url = "HelpNew.aspx?Type=" + Request["Type"] + "&DepartmentId=" + Request["DepartmentId"] + "&Pid=" + Request["Pid"] + "";
            Response.Redirect(url);
        }
        else if (Type == "2")
        {

            if (list.Count > 0)
            {
                Guid Aid = new Guid(Request["Aid"]);
                //删除全部
                List<AssaultSoldiers> dsoldiers = tdc.AssaultSoldiers.Where(o => o.AId == Aid).ToList<AssaultSoldiers>();
                tdc.AssaultSoldiers.DeleteAllOnSubmit<AssaultSoldiers>(dsoldiers);
                tdc.SubmitChanges();
                //重新添加
                List<AssaultSoldiers> soldiers = new List<AssaultSoldiers>();
                for (int i = 0; i < list.Count; i++)
                {
                    AssaultSoldiers soldier = new AssaultSoldiers();
                    soldier.AId = Aid;
                    soldier.Id = Guid.NewGuid();
                    soldier.SetDate = DateTime.Now;
                    soldier.SoldierId = list[i];
                    soldier.SetUserId = User.Identity.Name;

                    soldiers.Add(soldier);
                }
                tdc.AssaultSoldiers.InsertAllOnSubmit<AssaultSoldiers>(soldiers);
                tdc.SubmitChanges();
            }
            string url = "HelpNew.aspx?Type=" + Request["Type"] + "&DepartmentId=" + Request["DepartmentId"] + "&Aid="+Request["Aid"]+"";
            Response.Redirect(url);
        }
    }
    protected void lbtReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["UrlReferrer"].ToString());
    }
}
